package com.atguigu.flink0624.chapter07;

import com.atguigu.flink0624.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/13 9:27
 */
public class Flink14_Watermark_Sideout_2 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        //  水位值超过20和不超过20分别放入不同的流中
        SingleOutputStreamOperator<WaterSensor> mainStream = env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
            })
            .process(new ProcessFunction<WaterSensor, WaterSensor>() {
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<WaterSensor> out) throws Exception {
                    if (value.getVc() < 20) {
                        out.collect(value);
                    } else {
                        ctx.output(new OutputTag<WaterSensor>("over20"){}, value);
                    }
                }
            });
        
        mainStream.print("main");
        mainStream.getSideOutput(new OutputTag<WaterSensor>("over20"){}).print("over20");
        
        env.execute();
    }
    
}
